package com.android.equipment.utils

import com.android.equipment.bean.RealDataBean
import com.android.equipment.dao.Data
import com.android.equipment.dao.operate.DaoConfigModel
import com.android.equipment.dao.operate.DaoData
import com.blankj.utilcode.util.StringUtils
import org.json.JSONObject
import java.math.RoundingMode

object UiDataHandler {
    private val realDataList: MutableList<RealDataBean> = mutableListOf()
    fun handlerDefaultNodeData(deviceModelId: Long): MutableList<RealDataBean>{
        realDataList.clear()
        //查询出需要在页面显示的设备节点
        val showConfigModeList = DaoConfigModel.getInstance().getShowConfigModeByDeviceModelId(deviceModelId)
        showConfigModeList.forEach {
            val realDataBean = RealDataBean()
            realDataBean.node = it.node
            realDataBean.node_des = it.node_des
            realDataBean.node_unit = it.uni
            realDataBean.node_value = "--"
            realDataBean.relation_node = it.relation_node
            realDataList.add(realDataBean)
        }
        return realDataList
    }


    fun getLastTimeNodeData(deviceCode: String, deviceModelId: Long, beginTime: Long, endTime: Long?): MutableList<RealDataBean>{
        realDataList.clear()
        //先获取最新时间的该设备的数据
        val data: Data? = if (null == endTime){
            DaoData.getInstance().getProgressDataByDeviceCode(deviceCode,beginTime)
        }else {
            DaoData.getInstance().getCompletedDataByDeviceCode(deviceCode,beginTime,endTime)
        }

        if (null != data){
            val jsonObject = JSONObject(data.txt_json)
            jsonObject.keys().forEach { key ->
                //根据设备型号ID 和 node 查询出当前节点的配置
                val configModel = DaoConfigModel.getInstance()
                    .getConfigModelByDeviceIdAndNode(deviceModelId, key)
                if (configModel != null) { //如果配置表中存在该节点
                    if (configModel.is_show == 1) { //配置表中 设置的当前节点是显示状态
                        val realDataBean = RealDataBean()
                        realDataBean.node = key
                        realDataBean.node_des = configModel.node_des
                        realDataBean.node_unit = configModel.uni
                        if (configModel.is_bool == 1) {  //如果配置表中设置的is_bool是1  数据传过来的值是1 显示文字的报警  否则显示正常
                            realDataBean.node_value =
                                if (jsonObject.getLong(key) == 1L) configModel.true_val else configModel.false_val
                        } else {
                            realDataBean.node_value = jsonObject.get(key).toString()
                        }
                        if (configModel.is_decimal > 0 ) {
                            realDataBean.node_value =
                                jsonObject.get(key).toString().toBigDecimal()
                                    .setScale(configModel.is_decimal, RoundingMode.HALF_DOWN).toPlainString()
                        }

                        if (!StringUtils.isEmpty(configModel.relation_node)) {
                            val relationNodeValue = jsonObject.get(configModel.relation_node).toString()
                            val relationNodeConfig = DaoConfigModel.getInstance().getConfigModelByDeviceIdAndNode(deviceModelId, configModel.relation_node)
                            realDataBean.relation_node = configModel.relation_node
                            realDataBean.relation_node_value = if (relationNodeValue == 255.toString()) "未开启" else relationNodeValue
                            realDataBean.relation_node_des = relationNodeConfig.node_des
                            realDataBean.relation_node_unit = if (relationNodeValue == 255.toString()) "" else relationNodeConfig.uni
                        }
                        realDataList.add(realDataBean)
                    }
                }
            }
        }

        return realDataList
    }


}